AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

function ENT:SpawnFunction( id, tr )

	local trace = util.GetPlayerTrace( id, id:GetAimVector() )
	local tr = util.TraceLine( trace )
	local combinelock = ents.Create( "ts2_combinelock" )
	combinelock:SetPos( tr.HitPos + tr.HitNormal * 30 )
	combinelock:Spawn()
	combinelock:Activate()

end

function ENT:Initialize()

	self.ButtonSounds = {

	Sound( "buttons/combine_button1.wav" );
	Sound( "buttons/combine_button2.wav" );
	Sound( "buttons/combine_button3.wav" );
	Sound( "buttons/combine_button5.wav" );
	Sound( "buttons/combine_button7.wav" );

	}

	self.Entity:SetModel( "models/props_combine/combine_lock01.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	util.PrecacheSound( "buttons/combine_button_locked.wav" )

	self.Entity:SetNWBool( "Locked", true )
	self.Entity:SetNWBool( "UnLocked", false )
	self.Timer = CurTime() + 2.5

end

function ENT:Use( activator, caller )

	if( self.Timer < CurTime() ) then
	
		self.Timer = CurTime() + 1
		
		if( caller:IsCombineDefense() ) then
			
			self.Entity:EmitSound( table.Random( self.ButtonSounds ) )

			if( self.Entity:GetNWBool( "Locked" ) ) then
			
			   self.Entity:SetNWBool( "Locked", false )
				
			else
			
				self.Entity:SetNWBool( "Locked", true )
				
			end
			
		--[[elseif( ( not self.Entity:GetNWBool( "Locked" ) ) and caller:IsCitizen() ) then
		
			self.Entity:SetNWBool( "AttemptedOpen", true )
			self.Entity:SetNWBool( "Locked", true )
				
			timer.Simple( 5, function() self.Entity:SetNWBool( "Locked", false ) self.Entity:SetNWBool( "AttemptedOpen", false ) end )
			
			self.Entity:EmitSound( table.Random( self.ButtonSounds ) )
				
		else

			self.Entity:SetNWBool( "AttemptedOpen", true )
			self.Entity:SetNWBool( "Locked", true )
				
			timer.Simple( 5, function() self.Entity:SetNWBool( "AttemptedOpen", false ) end )
				
			self.Entity:EmitSound( table.Random( self.ButtonSounds ) )
				
			
		end	--]]
		
		end
		
	end	
	
end

function ENT:StartTouch(ent)

	if( ent:IsDoor() ) then

		self.Entity:Remove()
		
		local newdoorlock = ents.Create( "ts2_combinelock" )
			
		newdoorlock:SetPos( self.Entity:GetPos() );
		newdoorlock:SetAngles( self.Entity:GetAngles() )

		newdoorlock:Spawn()
		newdoorlock:SetParent( ent )
		newdoorlock:SetCollisionGroup(0)
		
	end	

end

function ENT:EndTouch()

end

local function LookDoor( xent )

	local doors = ents.FindInSphere( xent:GetPos(), 10 )
	
	for i = 1, table.getn( doors ) do
	
		if( doors[i]:GetClass() == "prop_door_rotating" ) then
		
			doors[i]:Fire( "lock", "", 0 )
			doors[i]:Fire( "close", "", 0 )		
				
		else
			
			doors[i]:Fire( "lock", "", 0 )
			doors[i]:Fire ("close", "", 0 )
				
		end
	end	
end

local function UnlockDoor( xent )

	local doors=ents.FindInSphere( xent:GetPos(), 10 )
	
	for i = 1, table.getn( doors ) do
		
		if( doors[i]:GetClass() == "prop_door_rotating" ) then
			
			doors[i]:Fire( "unlock", "", 0 )	
			
		else
			
			doors[i]:Fire( "unlock", "", 0 )
			
		end
			
	end
		
end

function ENT:Think()
	
	if( self.Entity:GetNWBool( "Locked" ) ) then
		LookDoor( self.Entity )
	else
		UnlockDoor( self.Entity )	
	end
	
end


